package www.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.test.bean.UserInfo;
import www.test.service.signService;
import www.test.utils.TokenProcess;

public class signServlet extends HttpServlet
{

	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		
		UserInfo domain = new UserInfo();
		String method = request.getParameter("method");
		if(method==null)
		{
			this.execute(request,response);
		}
		else
		{
			
			
			boolean b = isRepeatSubmit(request);
			if(b==true)
			{
				response.setHeader("content-type", "text/html;charset=UTF-8");
				response.getWriter().print("请不要重复提交");
				return ;
			}
			
			request.getSession().removeAttribute("token");
			 
			
			
			String username = request.getParameter("username");
			String password =request.getParameter("password");
			String age =request.getParameter("age");
			String email =request.getParameter("email");
			
			
			domain.setUsername(username);
			domain.setPassword(password);
			domain.setAge(age);
			domain.setEmail(email);
			//在数据库中插入信息
			try
			{
				boolean res = signService.insert(request,response,domain);
				if(!res)
				{
					response.setHeader("content-type", "text/html;charset=UTF-8");
					response.getWriter().print("数据插入失败，请重试！！");
				}
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			response.setHeader("content-type", "text/html;charset=UTF-8");

			response.getWriter().print("用户名为："+username+"<br/>");
			response.getWriter().print("密码为："+password+"<br/>");
			
			response.getWriter().print("年龄为："+age+"<br/>");
			response.getWriter().print("邮件为："+email+"<br/>");
			String cookie =request.getCookies().toString();
			String token = request.getParameter("token");
			response.getWriter().write("token:"+token);
			response.getWriter().write("cookie:"+cookie);
			
		}
		
	
		
		

		
	}

	
	private boolean isRepeatSubmit(HttpServletRequest request)
	{
		String client_token = request.getParameter("token");
		if(client_token==null)
		{
			return true ;
		}
		String server_token =(String) request.getSession().getAttribute("token");
		//如果当前用户session中不存在token ，则当前用户是重复提交了表单
		if(server_token==null)
		{
			return true;
		}
		if(!client_token.equals(server_token))
		{
			return true;
		}
		return false ;
	}


	private void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String token =TokenProcess.getInstance().makeToken();
		request.getSession().setAttribute("token", token);
		request.getRequestDispatcher("/login/sign.jsp").forward(request, response);
		
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

		doGet(request, response);
	}

}
